package gui.windows;

import gui.GUIHandler;
import gui.GUIManager;
import host.HostingHandler;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import room.Room;
import room.RoomCellRenderer;
import room.RoomListModel;
import util.DeckListItemListener;
import util.UtilHandler;
import app.AppGlobals;
import config.LauncherHelper;

@SuppressWarnings("serial")
public class GameListTab extends JPanel {
	
	public JFrame frame;
	private JTextField txtSearch;
	
	public GameListTab() {
		this.setLayout(null);
		this.setBounds(32, 102, 976, 471);
		
		JPanel panel_7 = new JPanel();
		panel_7.setOpaque(false);
		panel_7.setBackground(new Color(255, 255, 255));
		panel_7.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Unranked", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_7.setBounds(10, 11, 352, 421);
		this.add(panel_7);
		panel_7.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 22, 332, 346);
		panel_7.add(scrollPane);
		
		
		RoomListModel unrankedListModel = new RoomListModel();
		
		AppGlobals.unrankedListModel = unrankedListModel;
		JList<Room> listboxUnranked = new JList<Room>(unrankedListModel);
		listboxUnranked.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listboxUnranked.setDoubleBuffered(true);
		listboxUnranked.setCellRenderer(new RoomCellRenderer());
		scrollPane.setViewportView(listboxUnranked);
		GUIManager.serverTabUnrankedListbox = listboxUnranked;
		listboxUnranked.addMouseListener(new MouseAdapter() {
		    public void mouseClicked(MouseEvent evt) {
		        JList list = (JList)evt.getSource();
		        if (evt.getClickCount() == 2) {
		            int index = list.locationToIndex(evt.getPoint());
		            HostingHandler.joinGame(AppGlobals.unrankedListModel.get(index));         
		        } else if (evt.getClickCount() == 3) {   // Triple-click
		            int index = list.locationToIndex(evt.getPoint());
		            
		        }
		    }
		});
		
		JButton btnNewButton = new JButton("Quick");
		btnNewButton.setBackground(new Color(255, 255, 255));
		btnNewButton.setBounds(196, 387, 73, 23);
		panel_7.add(btnNewButton);
		
		JButton button = new JButton("Host");
		button.setBackground(new Color(255, 255, 255));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GUIHandler.launcherHostWindow(false);
			}
		});
		button.setBounds(269, 387, 73, 23);
		panel_7.add(button);
		
		JPanel panel_8 = new JPanel();
		panel_8.setOpaque(false);
		panel_8.setBackground(new Color(255, 255, 255));
		panel_8.setForeground(new Color(0, 0, 0));
		//panel_8.setBackground(SystemColor.menu);
		panel_8.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Ranked", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_8.setBounds(377, 11, 352, 421);
		this.add(panel_8);
		panel_8.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 22, 332, 346);
		panel_8.add(scrollPane_1);
		
		RoomListModel rankedListModel = new RoomListModel();
		AppGlobals.rankedListModel = rankedListModel;
		JList<Room> listboxRanked = new JList<Room>(rankedListModel);
		listboxRanked.setCellRenderer(new RoomCellRenderer());
		listboxRanked.setDoubleBuffered(true);
		listboxRanked.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane_1.setViewportView(listboxRanked);
		GUIManager.serverTabRankedListbox = listboxRanked;
		
		listboxRanked.addMouseListener(new MouseAdapter() {
		    public void mouseClicked(MouseEvent evt) {
		        JList list = (JList)evt.getSource();
		        if (evt.getClickCount() == 2) {
		            int index = list.locationToIndex(evt.getPoint());
		            HostingHandler.joinGame(AppGlobals.rankedListModel.get(index));         
		        } else if (evt.getClickCount() == 3) {   // Triple-click
		            int index = list.locationToIndex(evt.getPoint());
		            
		        }
		    }
		});
		
		JButton button_1 = new JButton("Host");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUIHandler.launcherHostWindow(true);
			}
		});
		button_1.setBackground(new Color(255, 255, 255));
		button_1.setBounds(269, 387, 73, 23);
		panel_8.add(button_1);
		
		JButton button_2 = new JButton("Quick");
		button_2.setBackground(new Color(255, 255, 255));
		button_2.setBounds(196, 387, 73, 23);
		panel_8.add(button_2);
		
		JPanel panel_9 = new JPanel();
		panel_9.setOpaque(false);
		panel_9.setBackground(new Color(255, 255, 255));
		//panel_9.setBackground(SystemColor.menu);
		panel_9.setBounds(739, 11, 222, 421);
		this.add(panel_9);
		panel_9.setLayout(null);
		
		txtSearch = new JTextField();
		txtSearch.setBounds(10, 0, 202, 20);
		txtSearch.setHorizontalAlignment(SwingConstants.CENTER);
		txtSearch.setText("Search");
		panel_9.add(txtSearch);
		txtSearch.setColumns(10);
		
		JPanel panel_10 = new JPanel();
		panel_10.setOpaque(false);
		panel_10.setBackground(new Color(255, 255, 255));

		//panel_10.setBackground(SystemColor.menu);
		panel_10.setBorder(new TitledBorder(null, "Server Details", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_10.setBounds(10, 31, 202, 156);
		panel_9.add(panel_10);
		panel_10.setLayout(null);
		
		JLabel lblOfRooms = new JLabel("# of Rooms");
		lblOfRooms.setBounds(10, 22, 86, 14);
		panel_10.add(lblOfRooms);
		
		JLabel lblOfUnranked = new JLabel("# of Unranked");
		lblOfUnranked.setBounds(10, 44, 86, 14);
		panel_10.add(lblOfUnranked);
		
		JLabel lblOfRanked = new JLabel("# of Ranked");
		lblOfRanked.setBounds(10, 69, 86, 14);
		panel_10.add(lblOfRanked);
		
		JLabel lblOfOpen = new JLabel("# of Open Rooms");
		lblOfOpen.setBounds(10, 94, 86, 14);
		panel_10.add(lblOfOpen);
		
		JLabel lblOfPlayers = new JLabel("# of Players");
		lblOfPlayers.setBounds(10, 119, 86, 14);
		panel_10.add(lblOfPlayers);
		
		JLabel label = new JLabel("0");
		GUIManager.serverTabRoomsLabel = label;
		label.setHorizontalAlignment(SwingConstants.TRAILING);
		label.setBounds(106, 22, 86, 14);
		panel_10.add(label);
		
		JLabel label_1 = new JLabel("0");
		GUIManager.serverTabUnrankedLabel = label_1;
		label_1.setHorizontalAlignment(SwingConstants.TRAILING);
		label_1.setBounds(106, 44, 86, 14);
		panel_10.add(label_1);
		
		JLabel label_2 = new JLabel("0");
		GUIManager.serverTabRankedLabel = label_2;
		label_2.setHorizontalAlignment(SwingConstants.TRAILING);
		label_2.setBounds(106, 69, 86, 14);
		panel_10.add(label_2);
		
		JLabel label_3 = new JLabel("0");
		GUIManager.serverTabOpenRoomsLabel = label_3;
		label_3.setHorizontalAlignment(SwingConstants.TRAILING);
		label_3.setBounds(106, 94, 86, 14);
		panel_10.add(label_3);
		
		JLabel label_4 = new JLabel("0");
		GUIManager.serverTabPlayersLabel = label_4;
		label_4.setHorizontalAlignment(SwingConstants.TRAILING);
		label_4.setBounds(106, 119, 86, 14);
		panel_10.add(label_4);
		
		JPanel panel_11 = new JPanel();
		panel_11.setOpaque(false);
		panel_11.setBackground(new Color(255, 255, 255));
		//panel_11.setBackground(SystemColor.menu);
		panel_11.setBounds(10, 237, 202, 173);
		panel_9.add(panel_11);
		panel_11.setLayout(null);
		
		JButton btnProfile = new JButton("Profile");
		btnProfile.setBounds(10, 139, 88, 23);
		panel_11.add(btnProfile);
		
		JButton btnLogout = new JButton("Logout");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//GUIHandler.launchLoginWindow();
				//frame.dispose();
			}
		});
		btnLogout.setBounds(104, 139, 88, 23);
		panel_11.add(btnLogout);
		
		JButton btnReplays = new JButton("Replays");
		btnReplays.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LauncherHelper.runGame("-r");
			}
		});
		btnReplays.setBounds(10, 105, 88, 23);
		panel_11.add(btnReplays);
		
		JButton btnOffline = new JButton("Offline");
		btnOffline.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LauncherHelper.runGame("");
			}
		});
		btnOffline.setBounds(104, 105, 88, 23);
		panel_11.add(btnOffline);
		
		JButton btnDeckEdit = new JButton("Deck Edit");
		btnDeckEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LauncherHelper.runGame("-d");
			}
		});
		btnDeckEdit.setBounds(10, 71, 88, 23);
		panel_11.add(btnDeckEdit);
		
		JButton btnOptions = new JButton("Options");
		btnOptions.setBounds(104, 71, 88, 23);
		panel_11.add(btnOptions);
		btnOptions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUIHandler.launcherOptionWindow();
			}
		});
		JCheckBox chckbxFilterActiveGames = new JCheckBox("Filter Active Games");
		chckbxFilterActiveGames.setSelected(true);
		chckbxFilterActiveGames.setEnabled(true);
		chckbxFilterActiveGames.setBounds(10, 7, 186, 23);
		panel_11.add(chckbxFilterActiveGames);
		GUIManager.serverTabFilterCheckbox = chckbxFilterActiveGames;

		//String[] deckList = UtilHandler.getDeckList();
		JComboBox comboBox = new JComboBox(UtilHandler.getDeckList());
		comboBox.addItemListener(new DeckListItemListener());
		comboBox.setBounds(10, 40, 182, 20);
		panel_11.add(comboBox);
	}

	
}
